﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;


using System.Threading;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using CCWin;
using System.Text.RegularExpressions;
using CRegex;

namespace weiqijiance
{
    public partial class xinhaochulibanxinzeng : CCSkinMain
    {
        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;
        

        public xinhaochulibanxinzeng()
        {
            InitializeComponent();
            readshezhi();
        }
        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }

        public void readshezhi()
        {

        }

        private void xinhaochulibanxinzeng_Load(object sender, EventArgs e)
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            #region 信号板1
            text_XHCLB1.Text = ReadValue("串口设置", "信号处理模块");
            che_JSLLKZ1.Checked = ReadValue("通道设置", "举升两路控制").Contains("是") ? true : false;
            //che_xhbxy1.Checked = ReadValue("通道设置", "信号板新协议").Contains("是") ? true : false;
            che_cscg.Checked = ReadValue("通道设置", "测功台配置车速工位").Contains("是") ? true : false;
            che_LiftContrary.Checked = ReadValue("通道设置", "举升取反").Contains("是") ? true : false;
            che_cshwyk.Checked = ReadValue("通道设置", "车速红外使用遥控").Contains("是") ? true : false;

            Version1.Text = ReadValue("通道设置", "信号板新协议版本");

            moni1.Text = ReadValue("通道设置", "模拟信号1");
            moni2.Text = ReadValue("通道设置", "模拟信号2");
            moni3.Text = ReadValue("通道设置", "模拟信号3");
            moni4.Text = ReadValue("通道设置", "模拟信号4");
            moni5.Text = ReadValue("通道设置", "模拟信号5");

            sudu1.Text = ReadValue("通道设置", "速度1");
            sudu2.Text = ReadValue("通道设置", "速度2");

            kongzhi1.Text = ReadValue("通道设置", "控制1");
            kongzhi2.Text = ReadValue("通道设置", "控制2");
            kongzhi3.Text = ReadValue("通道设置", "控制3");
            kongzhi4.Text = ReadValue("通道设置", "控制4");
            kongzhi5.Text = ReadValue("通道设置", "控制5");
            kongzhi6.Text = ReadValue("通道设置", "控制6");
            kongzhi7.Text = ReadValue("通道设置", "控制7");
            kongzhi8.Text = ReadValue("通道设置", "控制8");

            hongwai1.Text = ReadValue("通道设置", "红外1");
            hongwai2.Text = ReadValue("通道设置", "红外2");
            hongwai3.Text = ReadValue("通道设置", "红外3");
            hongwai4.Text = ReadValue("通道设置", "红外4");
            hongwai5.Text = ReadValue("通道设置", "红外5");
            hongwai6.Text = ReadValue("通道设置", "红外6");
            hongwai7.Text = ReadValue("通道设置", "红外7");
            hongwai8.Text = ReadValue("通道设置", "红外8");

            hongwaiDWSJ1.Text = ReadValue("通道设置", "红外定位时间1");
            hongwaiDWSJ2.Text = ReadValue("通道设置", "红外定位时间2");
            hongwaiDWSJ3.Text = ReadValue("通道设置", "红外定位时间3");
            hongwaiDWSJ4.Text = ReadValue("通道设置", "红外定位时间4");
            hongwaiDWSJ5.Text = ReadValue("通道设置", "红外定位时间5");
            hongwaiDWSJ6.Text = ReadValue("通道设置", "红外定位时间6");
            hongwaiDWSJ7.Text = ReadValue("通道设置", "红外定位时间7");
            hongwaiDWSJ8.Text = ReadValue("通道设置", "红外定位时间8");

            che_HW1.Checked = ReadValue("通道设置", "红外是否取反1").Contains("是") ? true : false;
            che_HW2.Checked = ReadValue("通道设置", "红外是否取反2").Contains("是") ? true : false;
            che_HW3.Checked = ReadValue("通道设置", "红外是否取反3").Contains("是") ? true : false;
            che_HW4.Checked = ReadValue("通道设置", "红外是否取反4").Contains("是") ? true : false;
            che_HW5.Checked = ReadValue("通道设置", "红外是否取反5").Contains("是") ? true : false;
            che_HW6.Checked = ReadValue("通道设置", "红外是否取反6").Contains("是") ? true : false;
            che_HW7.Checked = ReadValue("通道设置", "红外是否取反7").Contains("是") ? true : false;
            che_HW8.Checked = ReadValue("通道设置", "红外是否取反8").Contains("是") ? true : false;



            #endregion


            #region 信号板2
            text_XHCLB2.Text = ReadValue("串口设置", "信号处理模块2");
            che_JSLLKZ2.Checked = ReadValue("通道设置", "举升两路控制2").Contains("是") ? true : false;
            //che_xhbxy2.Checked = ReadValue("通道设置", "信号板新协议2").Contains("是") ? true : false;

            Version2.Text = ReadValue("通道设置", "信号板新协议版本2");

            moni6.Text = ReadValue("通道设置", "模拟信号6");
            moni7.Text = ReadValue("通道设置", "模拟信号7");
            moni8.Text = ReadValue("通道设置", "模拟信号8");
            moni9.Text = ReadValue("通道设置", "模拟信号9");
            moni10.Text = ReadValue("通道设置", "模拟信号10");

            sudu3.Text = ReadValue("通道设置", "速度3");
            sudu4.Text = ReadValue("通道设置", "速度4");

            kongzhi9.Text = ReadValue("通道设置", "控制9");
            kongzhi10.Text = ReadValue("通道设置", "控制10");
            kongzhi11.Text = ReadValue("通道设置", "控制11");
            kongzhi12.Text = ReadValue("通道设置", "控制12");
            kongzhi13.Text = ReadValue("通道设置", "控制13");
            kongzhi14.Text = ReadValue("通道设置", "控制14");
            kongzhi15.Text = ReadValue("通道设置", "控制15");
            kongzhi16.Text = ReadValue("通道设置", "控制16");

            hongwai9.Text = ReadValue("通道设置", "红外9");
            hongwai10.Text = ReadValue("通道设置", "红外10");
            hongwai11.Text = ReadValue("通道设置", "红外11");
            hongwai12.Text = ReadValue("通道设置", "红外12");
            hongwai13.Text = ReadValue("通道设置", "红外13");
            hongwai14.Text = ReadValue("通道设置", "红外14");
            hongwai15.Text = ReadValue("通道设置", "红外15");
            hongwai16.Text = ReadValue("通道设置", "红外16");

            hongwaiDWSJ9.Text = ReadValue("通道设置", "红外定位时间9");
            hongwaiDWSJ10.Text = ReadValue("通道设置", "红外定位时间10");
            hongwaiDWSJ11.Text = ReadValue("通道设置", "红外定位时间11");
            hongwaiDWSJ12.Text = ReadValue("通道设置", "红外定位时间12");
            hongwaiDWSJ13.Text = ReadValue("通道设置", "红外定位时间13");
            hongwaiDWSJ14.Text = ReadValue("通道设置", "红外定位时间14");
            hongwaiDWSJ15.Text = ReadValue("通道设置", "红外定位时间15");
            hongwaiDWSJ16.Text = ReadValue("通道设置", "红外定位时间16");

            che_HW9.Checked = ReadValue("通道设置", "红外是否取反9").Contains("是") ? true : false;
            che_HW10.Checked = ReadValue("通道设置", "红外是否取反10").Contains("是") ? true : false;
            che_HW11.Checked = ReadValue("通道设置", "红外是否取反11").Contains("是") ? true : false;
            che_HW12.Checked = ReadValue("通道设置", "红外是否取反12").Contains("是") ? true : false;
            che_HW13.Checked = ReadValue("通道设置", "红外是否取反13").Contains("是") ? true : false;
            che_HW14.Checked = ReadValue("通道设置", "红外是否取反14").Contains("是") ? true : false;
            che_HW15.Checked = ReadValue("通道设置", "红外是否取反15").Contains("是") ? true : false;
            che_HW16.Checked = ReadValue("通道设置", "红外是否取反16").Contains("是") ? true : false;
            #endregion



            
        }

        private void but_BaoCun1_Click(object sender, EventArgs e)
        {
            baocunshezhi1();

        }

        public void baocunshezhi1()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            //if (checkset(text_XHCLB1.Text))
            if (CRegex.C_Regex.IsCOM(text_XHCLB1.Text))
            {
                Writue("串口设置", "信号处理模块", text_XHCLB1.Text);
            }
            else
            {
                DialogResult result= MessageBox.Show(string.Format(@"当前信号处理板形式为：{0} " + "\n\r" + "正确串口格式为:COM(X:数字):19200(波特率）:0:0:1 " + "\n\r" +
              "实例:COM1:19200:8:0:1", text_XHCLB1.Text),"提示",MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    Writue("串口设置", "信号处理模块", text_XHCLB1.Text);
                }
            }
            

            Writue("通道设置", "举升两路控制", che_JSLLKZ1.Checked ? "是" : "否");

            Writue("通道设置", "举升取反", che_LiftContrary.Checked ? "是" : "否");

            Writue("通道设置", "测功台配置车速工位", che_cscg.Checked ? "是" : "否");
            Writue("通道设置", "车速红外使用遥控", che_cshwyk.Checked ? "是" : "否");

            //if (che_xhbxy1.Checked)//wwt
            //{
            //    Writue("通道设置", "信号板新协议", "是");
            //}
            //else
            //{
            //    Writue("通道设置", "信号板新协议", "否");
            //}


            Writue("通道设置", "信号板新协议版本", Version1.Text);



            Writue("通道设置", "模拟信号1", moni1.Text);
            Writue("通道设置", "模拟信号2", moni2.Text);
            Writue("通道设置", "模拟信号3", moni3.Text);
            Writue("通道设置", "模拟信号4", moni4.Text);
            Writue("通道设置", "模拟信号5", moni5.Text);

            Writue("通道设置", "速度1", sudu1.Text);
            Writue("通道设置", "速度2", sudu2.Text);

            Writue("通道设置", "控制1", kongzhi1.Text);
            Writue("通道设置", "控制2", kongzhi2.Text);
            Writue("通道设置", "控制3", kongzhi3.Text);
            Writue("通道设置", "控制4", kongzhi4.Text);
            Writue("通道设置", "控制5", kongzhi5.Text);
            Writue("通道设置", "控制6", kongzhi6.Text);
            Writue("通道设置", "控制7", kongzhi7.Text);
            Writue("通道设置", "控制8", kongzhi8.Text);

            Writue("通道设置", "红外1", hongwai1.Text);
            Writue("通道设置", "红外2", hongwai2.Text);
            Writue("通道设置", "红外3", hongwai3.Text);
            Writue("通道设置", "红外4", hongwai4.Text);
            Writue("通道设置", "红外5", hongwai5.Text);
            Writue("通道设置", "红外6", hongwai6.Text);
            Writue("通道设置", "红外7", hongwai7.Text);
            Writue("通道设置", "红外8", hongwai8.Text);


            Writue("通道设置", "红外定位时间1", hongwaiDWSJ1.Text);
            Writue("通道设置", "红外定位时间2", hongwaiDWSJ2.Text);
            Writue("通道设置", "红外定位时间3", hongwaiDWSJ3.Text);
            Writue("通道设置", "红外定位时间4", hongwaiDWSJ4.Text);
            Writue("通道设置", "红外定位时间5", hongwaiDWSJ5.Text);
            Writue("通道设置", "红外定位时间6", hongwaiDWSJ6.Text);
            Writue("通道设置", "红外定位时间7", hongwaiDWSJ7.Text);
            Writue("通道设置", "红外定位时间8", hongwaiDWSJ8.Text);


            Writue("通道设置", "红外是否取反1", che_HW1.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反2", che_HW2.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反3", che_HW3.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反4", che_HW4.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反5", che_HW5.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反6", che_HW6.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反7", che_HW7.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反8", che_HW8.Checked ? "是" : "否");

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "串口及通道设置.ini");

        }

        public void baocunshezhi2()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            

            if (checkset(text_XHCLB2.Text))
            {
                Writue("串口设置", "信号处理模块2", text_XHCLB2.Text);
            }
            else
            {
                DialogResult result = MessageBox.Show(string.Format(@"当前信号处理板形式为：{0} " + "\n\r" + "正确串口格式为:COM(X:数字):19200(波特率）:0:0:1 " + "\n\r" +
              "实例:COM1:19200:8:0:1", text_XHCLB2.Text), "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    Writue("串口设置", "信号处理模块2", text_XHCLB2.Text);
                }
            }

            Writue("通道设置", "举升两路控制2", che_JSLLKZ2.Checked ? "是" : "否");
            //if (che_xhbxy2.Checked)//wwt
            //{
            //    Writue("通道设置", "信号板新协议2", "是");
            //}
            //else
            //{
            //    Writue("通道设置", "信号板新协议2", "否");
            //}
             Writue("通道设置", "信号板新协议版本2", Version2.Text);



            Writue("通道设置", "模拟信号6", moni6.Text);
            Writue("通道设置", "模拟信号7", moni7.Text);
            Writue("通道设置", "模拟信号8", moni8.Text);
            Writue("通道设置", "模拟信号9", moni9.Text);
            Writue("通道设置", "模拟信号10", moni10.Text);

            Writue("通道设置", "速度3", sudu3.Text);
            Writue("通道设置", "速度4", sudu4.Text);

            Writue("通道设置", "控制9", kongzhi9.Text);
            Writue("通道设置", "控制10", kongzhi10.Text);
            Writue("通道设置", "控制11", kongzhi11.Text);
            Writue("通道设置", "控制12", kongzhi12.Text);
            Writue("通道设置", "控制13", kongzhi13.Text);
            Writue("通道设置", "控制14", kongzhi14.Text);
            Writue("通道设置", "控制15", kongzhi15.Text);
            Writue("通道设置", "控制16", kongzhi16.Text);

            Writue("通道设置", "红外9", hongwai9.Text);
            Writue("通道设置", "红外10", hongwai10.Text);
            Writue("通道设置", "红外11", hongwai11.Text);
            Writue("通道设置", "红外12", hongwai12.Text);
            Writue("通道设置", "红外13", hongwai13.Text);
            Writue("通道设置", "红外14", hongwai14.Text);
            Writue("通道设置", "红外15", hongwai15.Text);
            Writue("通道设置", "红外16", hongwai16.Text);


            Writue("通道设置", "红外定位时间9", hongwaiDWSJ9.Text);
            Writue("通道设置", "红外定位时间10", hongwaiDWSJ10.Text);
            Writue("通道设置", "红外定位时间11", hongwaiDWSJ11.Text);
            Writue("通道设置", "红外定位时间12", hongwaiDWSJ12.Text);
            Writue("通道设置", "红外定位时间13", hongwaiDWSJ13.Text);
            Writue("通道设置", "红外定位时间14", hongwaiDWSJ14.Text);
            Writue("通道设置", "红外定位时间15", hongwaiDWSJ15.Text);
            Writue("通道设置", "红外定位时间16", hongwaiDWSJ16.Text);


            Writue("通道设置", "红外是否取反9", che_HW9.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反10", che_HW10.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反11", che_HW11.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反12", che_HW12.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反13", che_HW13.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反14", che_HW14.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反15", che_HW15.Checked ? "是" : "否");
            Writue("通道设置", "红外是否取反16", che_HW16.Checked ? "是" : "否");

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "串口及通道设置.ini");
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            baocunshezhi2();
        }
        /// <summary>
        /// 串口设置检验格式
        /// </summary>
        /// <param name="sdata"></param>
        /// <returns></returns>
        public bool checkset(string sdata)
        {
            bool b = false;
            string tiaojian = @"^(COM\d):([0-9]*):(\d):(\d):(\d)$";
            Regex ret = new Regex(tiaojian);
            b = ret.IsMatch(sdata);
            return b;
        }
    }
}
